﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;


namespace user.admin.cyt369.com.services.Services
{
    public class UserService : admin.cyt369.com.services.ORM.BaseService<UserService>
    {
        public UserService() { }

        #region UserType
        public IQueryable<cyt369.com.services.Models.UserType> GetUserTypes(int page, int rows, ref int total, string name = null)
        {
            var items = base.Get<cyt369.com.services.Models.UserType>();
            if (!string.IsNullOrEmpty(name))
            {
                items = items.Where(c => c.Name.Contains(name));
            }

            return this.Pagination(page, rows, ref total, items, c => c.Id);
        }

        public cyt369.com.services.Models.UserType GetUserTypeByID(long id)
        {
            return base.GetOne<cyt369.com.services.Models.UserType>(c=>c.Id == id);
        }

        public bool SaveUserType(cyt369.com.services.Models.UserType userType)
        {
            return this.Save<cyt369.com.services.Models.UserType>(userType, c => c.Id == userType.Id);
        }

        public bool DeleteUserType(long id)
        {
            return base.Delete<cyt369.com.services.Models.UserType>(c => c.Id == id);
        }
        #endregion

        #region User
        public IQueryable<cyt369.com.services.Models.User> GetUsers(int page, int rows, ref int total, 
                                                                    string loginName = null, string fullName = null)
        {
            var items = base.Get<cyt369.com.services.Models.User>();
            if (!string.IsNullOrEmpty(fullName))
            {
                items = items.Where(c=>c.FullName.Contains(fullName));
            }

            if (!string.IsNullOrEmpty(loginName))
            {
                items = items.Where(c => c.UserName.Contains(loginName));
            }

            return this.Pagination(page, rows, ref total, items, c => c.Id);
        }

        public cyt369.com.services.Models.User GetUserByID(long id)
        {
            return base.GetOne<cyt369.com.services.Models.User>(c => c.Id == id);
        }

        #endregion

        #region UserLoginLogs
        public IQueryable<cyt369.com.services.Models.UserLoginLog> GetUserLoginLogs(int page, int rows, ref int total, string loginName = null)
        {
            var db = this.Ctx;
            var items = from o in db.Set<cyt369.com.services.Models.UserLoginLog>()
                        select o;

            if (!string.IsNullOrEmpty(loginName))
            {
                var users = from o in db.Set<cyt369.com.services.Models.User>()
                           where o.UserName.Contains(loginName)
                           select o.Id;

                items = from o in items
                        join u in users on o.UserID equals u
                        select o;
            }

            return this.Pagination(page, rows, ref total, items, c => c.Id);
        }

        public cyt369.com.services.Models.UserLoginLog GetUserLoginLogByID(long id)
        {
            return base.GetOne<cyt369.com.services.Models.UserLoginLog>(c => c.Id == id);
        }

        #endregion
    }
}
